package com.azier.kapacitor.script.node;

import com.azier.kapacitor.script.Script;
import com.azier.kapacitor.script.arg.Arg;
import com.azier.kapacitor.script.method.Method;
import com.azier.kapacitor.util.CollectionUtil;

import java.util.List;

/**
 * 1 * @Author: luoj
 * 2 * @Date: 2019/11/7 17:18
 * 3
 */
public abstract class AbstractNode implements Node, Script {

    final String name;

    protected Node headNode;

    protected Node nextNode;

    protected final List<Arg> args;

    /**
     * 不允许一个节点下调用相同的方法
     */
    final List<Method> methods;

    protected AbstractNode(final List<Arg> args, final List<Method> methods,final String name) {
        this.args = args;
        this.methods = methods;
        this.name = name;
    }

    @Override
    public void zetHeadNode(Node node){
        this.headNode = node;
    }
    @Override
    public Node getHeadNode(){
        return headNode;
    }
    @Override
    public String getNodeName(){
        return name;
    }

    @Override
    public Node getNextNode(){
        return nextNode;
    }

    @Override
    public void setNextNode(Node node){
        this.nextNode = node;
    }

    protected void setHeadNode(){
        if(this.headNode!=null&&this.nextNode!=null){
            this.nextNode.zetHeadNode(this.headNode);
        }
    }

    protected void addMethod(Method method){
        if(!this.methods.contains(method)){
            this.methods.add(method);
        }
    }

    @Override
    public StringBuilder append(StringBuilder stringBuilder){
        stringBuilder.append("|").append(name).append("(");
        if (CollectionUtil.isNotEmpty(args)) {
            for (int i = 0; i < args.size(); i++) {
                args.get(i).append(stringBuilder);
                if(i!=args.size()-1){
                    stringBuilder.append(",");
                }
            }
        }
        stringBuilder.append(")");
        if (CollectionUtil.isNotEmpty(methods)) {
            for (Method method : methods) {
                method.append(stringBuilder);
            }
        }

        if(nextNode!=null){
            nextNode.append(stringBuilder);
        }
        return stringBuilder;
    }

    void cess(){

    }

    /**
     * 获取脚本类型
     * @return
     */
    @Override
    public String getScriptType(){
        if(this.headNode!=null){
            if(this.headNode instanceof StreamNode){
                return Node.STREAM;
            }else {
                return Node.BATCH;
            }
        }else {
            if(this instanceof StreamNode){
                return Node.STREAM;
            }else if(this instanceof BatchNode){
                return Node.BATCH;
            }
        }
        return null;
    }
    /**
     * 获取自身脚本
     * @return
     */
    @Override
    public String getSelfScript(){

        StringBuilder stringBuilder = new StringBuilder();
        if(this instanceof StreamNode){
            stringBuilder.append(Node.STREAM);
        }else if(this instanceof BatchNode){
            stringBuilder.append(Node.BATCH);
        }else {
            stringBuilder.append("|").append(name).append("(");
            if (CollectionUtil.isNotEmpty(args)) {
                for (int i = 0; i < args.size(); i++) {
                    args.get(i).append(stringBuilder);
                    if(i!=args.size()-1){
                        stringBuilder.append(",");
                    }
                }
            }
            stringBuilder.append(")");
        }
        if (CollectionUtil.isNotEmpty(methods)) {
            for (Method method : methods) {
                method.append(stringBuilder);
            }
        }
        return stringBuilder.toString();
    }
    /**
     * 获取全部脚本，如果没有头节点，则从自身开始
     * @return
     */
    @Override
    public String getScript(){
        StringBuilder stringBuilder = new StringBuilder();
        if(this.headNode!=null){
            this.headNode.append(stringBuilder);
        }else {
            this.append(stringBuilder);
        }
        return stringBuilder.toString();
    }
}